package models;

import java.io.IOException;
import java.util.ArrayList;

import models.entity.Entity;
import models.power.Power;
import models.tent.Tent;
import models.tile.LakeTile;
import models.tile.NormalTile;
import models.tile.Tile;
import controllers.Game;

public class Stage {
	
	public ArrayList<Tile> stage = new ArrayList<Tile>();
	public int numXTiles, numYTiles;
	public int size;
	public int numXTilesInScreen;
	public Game game;
	
	public int tilelong;
	public int pixRestX, pixRestY;
	
	public Stage(){
		
	}
	
	public ArrayList<Tile> createMap(int xTiles, int yTiles, Game game){
		
		numXTiles = xTiles;
		numYTiles = yTiles;
		
		if(numYTiles > 1){
			tilelong = Game.HEIGHTSTAGE/numYTiles;
			pixRestY = Game.HEIGHTSTAGE%numYTiles;
		}else {
			tilelong = Game.HEIGHTSTAGE/3;
			pixRestY = 0;
		}
		size = numXTiles*tilelong+4*tilelong;

		numXTilesInScreen = Game.WIDTH/tilelong;
		pixRestX = Game.WIDTH%tilelong;
		
		int numX = 0, numY = 0;
		this.game = game;
		
		for(int k=0; k<yTiles; k++){
			LakeTile lTile = new LakeTile(0, k*tilelong, tilelong, 0);
			LakeTile lTile1 = new LakeTile((numXTiles*tilelong)+(2*tilelong), k*tilelong, tilelong, 1);
			if(k==0){
				try {
					lTile.addFrame("/LakeTop.png");
					lTile1.addFrame("/LakeTop.png");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else if(k==yTiles-1){
				try {
					lTile.addFrame("/LakeBottom.png");
					lTile1.addFrame("/LakeBottom.png");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				try {
					lTile.addFrame("/LakeMittle.png");
					lTile1.addFrame("/LakeMittle.png");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			stage.add(lTile);
			stage.add(lTile1);
		}
		
		for(int i=0; i<xTiles; i++){
			for(int j=0; j<yTiles; j++){
				NormalTile tile = new NormalTile(i*tilelong+2*tilelong,j*tilelong, numX, numY, tilelong);
				try {
					tile.addFrame("/Grass.png");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				stage.add(tile);
				numX++;
			}
			numY++;
		}
		
		return stage;
	}
	
    public int getSize() {
		return size;
	}

	@SuppressWarnings("unchecked")
	public void doScroll(int num) {
    	 
    	if(num == 0){
    		if(game.getInd()<(numXTiles*tilelong)+(4*tilelong)-(numXTilesInScreen*tilelong)-pixRestX){
	    		for(Tile t:stage){
	   				t.setPosX(t.getPosX()-10);
	   	 		}
	    		ArrayList<Entity> entities = (ArrayList<Entity>) game.entities.clone();
	    		for(Entity e:entities){
	    			e.setPosX(e.getPosX()-10);
	    			for(Power p:e.getPowers()){
	    				p.setPosX(p.getPosX()-10);
	    			}
	    		}
	    		ArrayList<Tent> tents = (ArrayList<Tent>) game.tents.clone();
	    		for(Tent t:tents){
	    			t.setPosX(t.getPosX()-10);
	    		}
	    		game.setInd(game.getInd()+10);
    		}
    	}else if(num == 1){
    		if(game.getInd()>pixRestX){
    			for(Tile t:stage){
    				t.setPosX(t.getPosX()+10);
    			}
    			ArrayList<Entity> entities = (ArrayList<Entity>) game.entities.clone();
    			for(Entity e:entities){
	    			e.setPosX(e.getPosX()+10);
	    			for(Power p:e.getPowers()){
	    				p.setPosX(p.getPosX()+10);
	    			}
	    		}
    			ArrayList<Tent> tents = (ArrayList<Tent>) game.tents.clone();
    			for(Tent t:tents){
	    			t.setPosX(t.getPosX()+10);
	    		}
    			game.setInd(game.getInd()-10);
    		}
    	}
    	
    }
		

	public int getNumXTiles() {
		return numXTiles;
	}

	public int getNumYTiles() {
		return numYTiles;
	}
	
	public ArrayList<Tile> getStage(){
		return stage;
	}

	
	public int getNumXTilesInScreen() {
		return numXTilesInScreen;
	}

	public void setNumXTilesInScreen(int numXTilesInScreen) {
		this.numXTilesInScreen = numXTilesInScreen;
	}


}
